package com.noble.glamour.ui.user.paygoods.diamond;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.noble.library.log.PLogger;
import com.noble.library.utils.FileUtil;
import com.noble.glamour.R;
import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.library.view.base.BaseDialogFragment;
import com.noble.glamour.module.util.UIShow;
import com.noble.glamour.ui.user.paygoods.GoodsConstant;
import com.noble.glamour.ui.user.paygoods.GoodsListPanel;
import com.noble.glamour.ui.user.paygoods.GoodsPayTypePanel;
import com.noble.glamour.ui.user.paygoods.bean.PayGood;
import com.noble.glamour.ui.user.paygoods.bean.PayGoods;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 钻石底部弹框
 * Created by Su on 2017/8/22.
 */
public class GoodsDiamondBottomDlg extends BaseDialogFragment implements View.OnClickListener {
    private PayGoods payGoods;  // 商品信息
    private GoodsListPanel goodsPanel; // 商品列表
    private GoodsPayTypePanel payTypePanel; // 支付方式

    public GoodsDiamondBottomDlg() {
        settWindowAnimations(R.style.AnimDownInDownOutOverShoot);
        setGravity(Gravity.BOTTOM);
        setDialogSizeRatio(-2, -1);
        setCancelable(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.f1_bottom_diamond_dlg);
        initList();
        initView();
        return getContentView();
    }

    private void initView() {
        findViewById(R.id.btn_cancel).setOnClickListener(this);
        findViewById(R.id.btn_recharge).setOnClickListener(this);
        TextView ycoin_remain = (TextView) findViewById(R.id.ycoin_remain);
        ycoin_remain.setText(String.valueOf(ModuleMgr.getCenterMgr().getMyInfo().getDiamand()));

        // 商品
        LinearLayout container = (LinearLayout) findViewById(R.id.goods_container);
        goodsPanel = new GoodsListPanel(getContext(), GoodsConstant.DLG_DIAMOND_VIDEO);
        container.addView(goodsPanel.getContentView());
        goodsPanel.refresh(payGoods.getCommodityList());

        // 支付方式
        LinearLayout payContainer = (LinearLayout) findViewById(R.id.pay_type_container);
        payTypePanel = new GoodsPayTypePanel(getContext(), GoodsConstant.PAY_TYPE_NEW);
        payContainer.addView(payTypePanel.getContentView());
    }

    private void initList() {
        if (payGoods == null)
            payGoods = new PayGoods();

        String json = FileUtil.getFromAssets(App.getActivity(), "info_goods.json");
        try {
            if (TextUtils.isEmpty(json)) return;
            JSONObject jsonObject = new JSONObject(json);

            if (jsonObject.has("diamond"))
                payGoods.parseJson(jsonObject.optString("diamond"));
        } catch (JSONException e) {
            PLogger.printThrowable(e);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_recharge:  // 充值
                PayGood payGood = payGoods.getCommodityList().get(goodsPanel.getPosition());
                UIShow.showPayAlipayt(getActivity(), payGood.getId(), payTypePanel.getPayType(), 0, "");
                dismiss();
                break;

            case R.id.btn_cancel:
                dismiss();
                break;
        }
    }
}